package com.sz.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.D;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Status;

import com.diboot.file.controller.BaseExcelFileController;
import com.diboot.file.excel.listener.ReadExcelListener;
import com.diboot.file.util.ExcelHelper;

import com.sz.mes.entity.ReworkHours;
import com.sz.mes.dto.ReworkHoursDTO;
import com.sz.mes.service.ReworkHoursService;
import com.sz.mes.vo.ReworkHoursListVO;
import com.sz.mes.excel.model.ReworkHoursExportModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 返修工时 Excel处理Controller
 * @author hem
 * @version 1.0
 * @date 2024-11-28
 * Copyright © szhs
 */
@RestController
@RequestMapping("/reworkHours/excel")
@BindPermission(name = "返修工时导入导出", code = "ReworkHoursExcel")
@Slf4j
public class ReworkHoursExcelController extends BaseExcelFileController {

    @Autowired
    private ReworkHoursService reworkHoursService;

    
    /**
     * 返修工时列表导出
     *
     * @param response
     * @return
     * @throws Exception
     */
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/export")
    public JsonResult export(ReworkHoursDTO reworkHoursDto, @RequestParam(value = "columns", required = false) List<String> columns,
    												 HttpServletResponse response) throws Exception{
        QueryWrapper<ReworkHours> queryWrapper = super.buildQueryWrapperByQueryParams(reworkHoursDto);
        List<ReworkHoursListVO> reworkHoursList = reworkHoursService.getViewObjectList(queryWrapper, null, ReworkHoursListVO.class);
        if (V.isEmpty(reworkHoursList)) {
            return new JsonResult(Status.FAIL_OPERATION, "返修工时列表为空，导出失败");
        }
        String fileName = "返修工时列表导出_" + D.today() + ".xlsx";
        List<ReworkHoursExportModel> dataList = this.entityList2ExcelList(reworkHoursList);
        ExcelHelper.exportExcel(response, fileName, ReworkHoursExportModel.class, columns, dataList);
        return null;
    }

    /**
     * 实体列表转excel列表
     *
     * @param reworkHoursList
     * @return
     */
    private List<ReworkHoursExportModel> entityList2ExcelList(List<ReworkHoursListVO> reworkHoursList) {
        if (V.isEmpty(reworkHoursList)) {
            return Collections.emptyList();
        }
        return BeanUtils.convertList(reworkHoursList, ReworkHoursExportModel.class);
    }

    @Override
    protected ReadExcelListener getExcelDataListener() {
        return null;
    }
}